<?php

/**
 * Template for rendering one
 * answer.
 *
 * Need a way to add "Accept" link
 * IF viewer is also question owner
 * We can easily do this with javascript
 * by checking viewer ID in meta
 * and comparing to owner-id
 * of question. But it's not so
 * easy on server side because we loop
 * over mongo cursor, it does not know
 * who is a viewer, so...
 * We can pass callback function but
 * right now we don't have a good way
 * to pass callback to ::loop method
 * and even if we do, we would need to apply
 * the callback to each iteration, not very
 * effecient.
 * 
 */
class tplAnswer extends StudyBuddy\Template\Template {

    protected static function func(&$a) {
        if (array_key_exists('a_edited', $a)) {
            $aEdited = end($a['a_edited']);
            $aEdited['edited'] = $a['edited'];

            $a['edits'] = \tplEditedby::parse($aEdited, false);
        }

        if (!empty($a['hts'])) {
            $a['hts_ago'] = \StudyBuddy\TimeAgo::format(new \DateTime($a['hts']));
        }

        if (!empty($a['i_del_ts'])) {
            $a['deleted'] = ' deleted';
        }

        if (!empty($a['a_deleted'])) {
            $a['deletedby'] = \tplDeletedby::parse($a['a_deleted'], false);
        }

        if (!empty($a['a_comments'])) {
            /**
             * Closure function
             * to pass resource_id 
             * and author id of this
             * Answer to the tplComments
             * This way we don't have to store
             * duplicate data in each comment
             * element and still be able to
             * have access to these 2 important
             * fields in the tplComments template
             * We going to need id or resource owner
             * in order to add it to the "reply" link
             * in the form of class uid-$uid
             * 
             */
            $rid = $a['_id'];
            $uid = $a['i_uid'];
            $reply = $a['reply'];
            $reply_t = $a['reply_t'];

            $f = function(&$data) use ($rid, $uid, $reply, $reply_t) {
                        $data['resource_id'] = $rid;
                        $data['owner_id'] = $uid;
                        $data['reply'] = $reply;
                        $data['reply_t'] = $reply_t;
                    };

            $a['comments_html'] = tplComment::loop($a['a_comments'], true, $f);
        }
    }

    protected static $vars = array(
        '_id' => '', // 1
        'b' => '', // 2
        'ulink' => '', // 3
        'avtr' => '', // 4
        'hts' => '', // 5
        'i_votes' => '', // 6
        'i_uid' => '0', // 7 // answer author id
        'accepted_text' => '', //8
        'accepted_class' => 'accept_answer', // 9
        'vote_up' => "\xE2\x87\xA7", // 10 \xE2\x87\xA7
        'vote_down' => "\xE2\x87\xA9", //11
        'accept_link' => '&nbsp', // 12,
        'accepted' => '', //13
        'i_flags' => '', // 14
        'edits' => '', // 15
        'deleted' => '', //16
        'deletedby' => '', //17
        'comments_html' => '', //18
        'edit_delete' => '', // 19
        'i_comments' => '0', // 20
        'nocomments' => '', //21
        'i_lm_ts' => '0', // 22
        'add_comment' => 'add comment', //23
        'hts_ago' => '', //24
		'username' => '', //25
    );
    protected static $tpl = '
	<div class="box">
		<div class="box-left">
			<table class="tbl-boxl" cellpadding="0" cellspacing="0">	                                        
				<tr valign="top">
					<!--<td align="center"><a id="upvote-%1$s" title="I like this answer (click again to cancel)" class="ttt ajax vote thumbup" href="/ansvote/%1$s/up"><img alt="" src="/images/2/icon/star.gif" /></a></td>
					<td id="score%1$s" align="center" class="qscore boxl-tdn">%6$s</td>-->
					<td align="center">
                        <div class="votebtns cb" id="vote%1$s">
                            <a id="upvote-%1$s" title="I like this answer (click again to cancel)" class="ttt ajax vote thumbup" href="/ansvote/%1$s/up" rel="nofollow"></a>
                            <div id="score%1$s" class="qscore" style="padding-left:0; color:#777">%6$s</div>
                            <a id="downvote-%1$s" title="I dont like this answer (click again to cancel)" class="ttt ajax vote thumbdown down" href="/ansvote/%1$s/down" rel="nofollow"></a>
                        </div>
                    </td>
                    <td align="center">
                        <div class="boxr-bot-img">
                            <a href="/users/%7$s/%25$s" target="blank"><img alt="" src="%4$s" width="30px" height="30px" /></a>
                        </div>
                    </td>
				</tr>	
				<tr colspan="2">
					<td align="center">
						<div class="acceptit anstype">%13$s</div>
						<div class="acceptit">%12$s</div>
					</td>
				</tr>
			</table>
		</div>
		<div class="box-right">
			<div class="boxr-top" id="ansbody-%1$s">
				%2$s
			</div>
			<div class="boxr-bot">
				<!--<div class="boxr-bot-img">
					<a href="JavaScript:void(0)"><img alt="" src="%4$s" width="26" height="26" /></a>
				</div>-->
				<div class="boxr-bot-txt">
					answered by <a href="/users/%7$s/%25$s" target="blank">%25$s</a> %24$s | <span class="qa_abuse aabuse"><span id="res_%1$s" class="abuse ajax" style="cursor:pointer">Report abuse</span></span>
				</div>
				<div class="boxr-bot-btn">
					<a href="#" class="answerbutton ajax com_link" id="comlink_%1$s" style="border:none"><span class="boxr-btn" style="border:none; background-color:transparent"></span></a>
				</div>
			</div>
			<div class="answer controls uid-%7$s" id="res_%1$s">
			</div>
			<div class="comments%21$s i_comments_%20$s" id="comments-%1$s">
				%18$s
			</div>
		</div>
	</div>';

}
